package net.huashitong.supervision.task.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.task.dao.TaskMainDao;
import net.huashitong.supervision.task.dao.TaskSubDao;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.entity.TaskSub;
import net.huashitong.supervision.task.entity.TaskUnit;
import net.huashitong.supervision.task.filter.TaskSubFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.naming.event.ObjectChangeListener;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/6
 * Time: 18:17
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class TaskSubService extends StringPKService<TaskSub> {
    @Autowired
    private SysTableService tableService;
    @Autowired
    private TaskSubDao taskSubDao;
    @Autowired
    private TaskMainDao taskMainDao;
    @Autowired
    private TimeLimitService timeLimitService;
    @Autowired
    @Lazy(true)
    private TaskUnitService taskUnitService;

    @Override
    protected BaseDao<TaskSub, String> getDao() {
        return taskSubDao;
    }

    @Override
    protected void beforeSave(TaskSub obj) {
        obj.setSysId(tableService.updateMaxSysId("t_task_sub", null));
    }
    public void save(TaskSub obj, String userId){
        Date date = new Date();
        String date1 = net.huashitong.supervision.task.util.HolidayUtils.sdf1.format(date);
        String time = net.huashitong.supervision.task.util.HolidayUtils.sdf2.format(date);
        obj.setSysId(tableService.updateMaxSysId("t_task_sub",null));
        obj.setCreateUser(userId);
        obj.setUpdUser(userId);
        obj.setCreateDate(date1);
        obj.setCreateTime(time);
        obj.setUpdDate(date1);
        obj.setUpdTime(time);
        getDao().save(obj);
    }
    public List<TaskSub> queryFilter(TaskSubFilter taskSubFilter) {
        return taskSubDao.queryFilter(taskSubFilter);
    }

    public void batchDeleteIds(List<String> ids) {
        taskSubDao.batchDeleteTaskIds(ids);
        TaskSubFilter f = new TaskSubFilter();
        for (String id : ids) {
            f.setTaskMainId(id);
            //删除当前子任务的时限表记录
            List<TaskSub> taskSubList = this.queryFilter(f);
            if (ObjectUtils.isNotBlank(taskSubList)) {
                for (TaskSub taskSub : taskSubList) {
                    timeLimitService.deleteBySubId(taskSub.getSysId());
                }
            }
        }
    }

    public String deleteSub(String subId) {
        taskSubDao.deleteById(subId);
        List<TaskUnit> taskUnitList = taskUnitService.getBySubId(subId);
        for (TaskUnit taskUnit : taskUnitList) {
            taskUnitService.delete(taskUnit);
        }
        return "200";
    }

    public List<TaskSub> getByMainId(String id) {
        return taskSubDao.getByMainId(id);
    }
    public List<TaskSub> getByMainIdDel(String id) {
        return taskSubDao.getByMainIdDel(id);
    }
}
